(ns lr.ray)
;;=====================================
;; Define vector operations
;;=====================================

(defn func-vec [func]
  (fn [[a b c] [p q r]]
    (let [a (double a)
	  b (double b) 
	  c (double c)
	  p (double p)
	  q (double q)
	  r (double r)]
    [(func a p) (func b q) (func c r)])))

(def v+ (func-vec +))
(def v- (func-vec -))
(def v* (func-vec *))
(defn v*-scalar [[x y z] b]
  (let [b (double b)
	x (double x), y (double y), z (double z)]
     [(* b x) (* b y) (* b z)]))

(defn dot "Dot product" [[a b c] [x y z]]
  (+ (* a x) (* b y) (* c z)))

(defn norm [v] (java.lang.Math/sqrt (dot v v)))

(defn normalize [v] (v*-scalar v (/ 1 (norm v))))


;;======== Definition of a ray,
;;  A map of two 3-vectors.
(defstruct ray :origin :direction)